package com.tdam2011.grupo05.database.dao;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

import com.google.common.collect.Lists;
import com.tdam2011.grupo05.database.content.TdamContentProvider;
import com.tdam2011.grupo05.entities.message.MessageWeb;
import com.tdam2011.grupo05.entities.message.MessageWeb.State;
import com.tdam2011.grupo05.entities.message.MessageWeb.Type;

public class MessageWebDao implements Dao<MessageWeb> {

	static final String[] PROJECTION = { MessageWeb.COL_ID,
			MessageWeb.COL_ID_THREAD, MessageWeb.COL_RESULT,
			MessageWeb.COL_STATE, MessageWeb.COL_TIMESTAMP,
			MessageWeb.COL_TEXT, MessageWeb.COL_TYPE };

	private final ContentResolver cr;

	public MessageWebDao(Context context) {
		cr = context.getContentResolver();
	}

	@Override
	public Cursor getCursor(String selection, String[] selectionArgs,
			String sortOrder) {
		return cr.query(TdamContentProvider.MESSAGE_CONTENT_URI, PROJECTION,
				selection, selectionArgs, sortOrder);
	}

	@Override
	public long save(MessageWeb m) {

		final ContentValues cv = new ContentValues();
		cv.put(MessageWeb.COL_ID_THREAD, m.getIdThread());
		cv.put(MessageWeb.COL_TEXT, m.getText());
		cv.put(MessageWeb.COL_TYPE, m.getType().name());
		cv.put(MessageWeb.COL_TIMESTAMP, m.getTimestamp().getTime());
		cv.put(MessageWeb.COL_RESULT, m.getResult());
		cv.put(MessageWeb.COL_STATE, m.getState().name());
		Uri u = cr.insert(TdamContentProvider.MESSAGE_CONTENT_URI, cv);

		return ContentUris.parseId(u);

	}

	@Override
	public void update(MessageWeb m) {
		final ContentValues cv = new ContentValues();
		cv.put(MessageWeb.COL_ID, m.getId());
		cv.put(MessageWeb.COL_ID_THREAD, m.getIdThread());
		cv.put(MessageWeb.COL_TEXT, m.getText());
		cv.put(MessageWeb.COL_TYPE, m.getType().name());
		cv.put(MessageWeb.COL_TIMESTAMP, m.getTimestamp().getTime());
		cv.put(MessageWeb.COL_RESULT, m.getResult());
		cv.put(MessageWeb.COL_STATE, m.getState().name());
		cr.update(TdamContentProvider.MESSAGE_CONTENT_URI, cv, BaseColumns._ID
				+ "= ?", new String[] { String.valueOf(m.getId()) });
	}

	@Override
	public void delete(MessageWeb m) {
		if (m.getId() > 0) {
			cr.delete(TdamContentProvider.MESSAGE_CONTENT_URI, BaseColumns._ID
					+ "=?", new String[] { String.valueOf(m.getId()) });
		}
	}

	@Override
	public MessageWeb get(long id) {
		MessageWeb m = null;
		Cursor c = cr.query(TdamContentProvider.MESSAGE_CONTENT_URI,
				PROJECTION, BaseColumns._ID + "= ?",
				new String[] { String.valueOf(id) }, "1");
		if (c.moveToFirst()) {
			m = this.buildMessageFromCursor(c);
		}
		if (!c.isClosed()) {
			c.close();
		}
		return m;
	}

	@Override
	public List<MessageWeb> getAll() {
		List<MessageWeb> mList = Lists.newArrayList();
		Cursor c = cr.query(TdamContentProvider.MESSAGE_CONTENT_URI,
				PROJECTION, null, null, "1");
		if (c.moveToFirst()) {
			do {
				final MessageWeb m = buildMessageFromCursor(c);
				if (m != null) {
					mList.add(m);
				}
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		return mList;
	}

	public LinkedList<MessageWeb> getUnsendedMessages() {
		LinkedList<MessageWeb> mList = Lists.newLinkedList();
		String customSelection = MessageWeb.COL_STATE + "=? OR "
				+ MessageWeb.COL_STATE + "=?";
		String[] customArgs = { State.UNSENDED.toString(), State.ERROR.toString() };
		Cursor c = cr.query(TdamContentProvider.MESSAGE_CONTENT_URI,
				PROJECTION, customSelection, customArgs, null);
		if (c.moveToFirst()) {
			do {
				final MessageWeb m = buildMessageFromCursor(c);
				if (m != null) {
					mList.add(m);
				}
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		return mList;
	}

	public MessageWeb buildMessageFromCursor(Cursor c) {
		if (null != c) {
			final MessageWeb m = new MessageWeb();
			m.setId(c.getLong(c.getColumnIndex(MessageWeb.COL_ID)));
			m.setIdThread(c.getLong(c.getColumnIndex(MessageWeb.COL_ID_THREAD)));
			m.setResult(c.getString(c.getColumnIndex(MessageWeb.COL_RESULT)));
			m.setState(State.valueOf(c.getString(c
					.getColumnIndex(MessageWeb.COL_STATE))));
			m.setText(c.getString(c.getColumnIndex(MessageWeb.COL_TEXT)));
			m.setTimestamp(new Date(c.getLong(c
					.getColumnIndex(MessageWeb.COL_TIMESTAMP))));
			m.setType(Type.valueOf(c.getString(c
					.getColumnIndex(MessageWeb.COL_TYPE))));

			return m;

		} else {
			return null;
		}

	}

}
